package cn.itsource.pethome.system.dept.controller;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.dept.domain.Department;
import cn.itsource.pethome.system.dept.query.DepartmentQuery;
import cn.itsource.pethome.system.dept.service.IDepartmentService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
//这里是说明你这个类是干什么用的
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //@RequestMapping(value = "/list",method = RequestMethod.GET) //方式一 公司里面要用
    //@GetMapping("/list")
    @PostMapping("/list")
    @PreAuthorize(name = "获取部门所有信息",value = "dept:all")
    @ApiOperation(value = "获取所有的部门信息" )
    public List<Department> findAll(){
       return departmentService.findAll();
    }

    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询部门信息",value = "dept:findOne")
    @ApiOperation(value = "根据id查询部门信息")
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            Department one = departmentService.findOne(id);
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 他们都用来处理json的
     * @RequestBody 是前端传进来的
     * @ResponseBody 是后端返回给前端的
     *
     *
     * @param query
     * @return
     */
    @PostMapping("/pagelist")
    @ApiOperation(value = "分页查询")
    @PreAuthorize(name = "部门列表",value = "dept:list")
    public AjaxResult findQuery(@RequestBody DepartmentQuery query){
        try {
            List<Department> byQuery = departmentService.findByQuery(query);
            Long count = departmentService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    @PreAuthorize(name = "删除部门",value = "dept:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }


    @PutMapping
    @PreAuthorize(name = "保存部门",value = "dept:save")
    @ApiOperation(value = "保存部门信息【有id则为修改】")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() != null){
                departmentService.update(department);
            }else {
                departmentService.add(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    // {}
    //[{},{}]
    //[1,2]
    //批量
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除部门")
    @PreAuthorize(name = "批量删除部门",value = "dept:batchDelete")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Department> getDeptTree(){
        return departmentService.deptTree();
    }
}
